package org.lendoo.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.HistoryCart;
import org.lendoo.web.entity.RelOrderGoods;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.pojo.common.ServiceResult;
import org.springframework.stereotype.Service;
import org.lendoo.web.mapper.OrderMapper;
import org.lendoo.web.entity.Order;
import org.lendoo.web.entity.Cart;
import org.lendoo.web.service.OrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * OrderService接口的实现类
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 新增订单，返回订单id
     * @param order 订单表
     * @return 添加成功返回1，添加失败返回-1
     */
    @Transactional
    @Override
    public ServiceResult<Integer> add(Order order) {
        // 添加订单
        baseMapper.insert(order);
        int orderId = order.getId();

         QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id", order.getUserId());
        List<Cart> cartList = serviceHelper.getCartMapper().selectList(cartQueryWrapper);
        if (null != cartList) {
            for (int i =0; i<cartList.size(); i++){
                Cart cart = cartList.get(i);

                // 向表rel_order_goods中插入记录
                RelOrderGoods relOrderGoods = new RelOrderGoods();
                relOrderGoods.setOrderId(orderId);
                relOrderGoods.setQuantity(cart.getQuantity());
                relOrderGoods.setGoodsId(cart.getGoodsId());
                relOrderGoods.setUserId(order.getUserId());
                relOrderGoods.setCreateTime(new Date());
                relOrderGoods.setUpdateTime(new Date());
                serviceHelper.getRelOrderGoodsMapper().insert(relOrderGoods);

                // 向history_cart表中插入记录
                HistoryCart historyCart = new HistoryCart();
                historyCart.setCartId(cart.getId());
                historyCart.setQuantity(cart.getQuantity());
                historyCart.setGoodsId(cart.getGoodsId());
                historyCart.setSelected(cart.getSelected());
                historyCart.setUserId(cart.getUserId());
                historyCart.setCreateTime(cart.getCreateTime());
                historyCart.setUpdateTime(cart.getUpdateTime());
                historyCart.setArchiveTime(new Date());
                serviceHelper.getHistoryCartMapper().insert(historyCart);

                // 删除cart表中的记录
                serviceHelper.getCartMapper().deleteById(cart.getId());
            }
        }

        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), orderId, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 批量导入
     * @param orderList 订单表（列表）
     * @return 批量导入成功返回1，批量导入失败返回-1
     */
    @Override
    public ServiceResult<Boolean> batchAdd(List<Order> orderList) {
        baseMapper.insertBatchSomeColumn(orderList);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 根据id更新
     * @param order 订单表
     * @return 更新成功返回1，更新失败返回-1
     */
    @Override
    public ServiceResult<Boolean> updateForId(Order order) {
        baseMapper.updateById(order);
        return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 根据id删除
     * @param id 主键
     * @return 删除成功返回1，删除失败返回-1
     */
    @Override
    public ServiceResult<Boolean> deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return new ServiceResult<>(ServiceResultEnum.DELETE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 根据id查询
     * @param id 主键
     * @return 返回查询结果
     */
    @Override
    public ServiceResult<Order> selectById(Serializable id) {
        Order order = baseMapper.selectById(id);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), order, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     * @param order 订单表
     * @param pageNo 页号
     * @param pageSize 每页行数
     * @return 返回查询结果列表
     */
    @Override
    public ServiceResult<PageInfo<Order>> page(Order order, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Order> orderList = baseMapper.selectList(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), new PageInfo<>(orderList), Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查询所有记录
     * @return
     */
    @Override
    public ServiceResult<List<Order>> findAll() {
        List<Order> orderList = baseMapper.selectList(null);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), orderList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }
}
